package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.MapService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/map")
public class MapController {

    //远程调用MapService
    @Reference
    private MapService mapService;

    //查询数据库地址
    @RequestMapping("findAddress")
    private List<Address> findAddress() {
        List<Address> addresses = mapService.findAddress();
        return addresses;
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = mapService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;
    }

    //新增地址项
    @RequestMapping("/addAddress")
    public Result addAddress(@RequestBody Address address) {
        String message = "新增地址失败";
        try {
            Map map = mapService.add(address);
            boolean flag = (boolean) map.get("flag");
            message = (String) map.get("message");
            return new Result(flag, message);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, message);
        }

    }

    //删除地址项
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        String message = "删除地址失败";
        try {
            Map map = mapService.deleteById(id);
            boolean flag = (boolean) map.get("flag");
            message = (String) map.get("message");
            return new Result(flag, message);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, message);
        }
    }
}
